﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;

/// <summary>
/// Summary description for cartBus
/// </summary>
public class cartBus
{
    public List<cart> listcart {get;private set; }
    public static readonly cartBus carts;
    public orderDAL ordal; 
	protected cartBus()
	{
        ordal = new orderDAL();
		//
		// TODO: Add constructor logic here
		//
	}
    static cartBus()
    {
        if(HttpContext.Current.Session["listcart"]==null)
        {
            carts=new cartBus();
            carts.listcart=new List<cart>();
            HttpContext.Current.Session["listcart"]=carts;
        }else{
            carts=(cartBus)HttpContext.Current.Session["listcart"];
        }
    }
    public void AddCart(int product_id,int quantity)
    {
        cart newcart = new cart(product_id);
        if(listcart.Contains(newcart)){
            foreach (cart c in listcart)
            {
                if (c.Equals(newcart))
                {
                    c.Quantity = c.Quantity + quantity;
                    return;
                }
                
            }
        }
        else
        {
            newcart.Quantity = quantity;
            listcart.Add(newcart);
        }
        
    }
    public void setQuantityProduct(int product_id,int quantity)
    {
        cart newcart = new cart(product_id);
        if (listcart.Contains(newcart))
        {
            foreach (cart c in listcart)
            {
                if (c.Equals(newcart))
                {
                    c.Quantity = quantity;
                    return;
                }

            }
        }
        else
        {
            listcart.Add(newcart);
        }
        
    }
   
    public void RemoveItem(int _productId)
    {
        cart removedItem = new cart(_productId);
        listcart.Remove(removedItem);
    }
    public double subtotal()
    {
        double total = 0;
        foreach (cart c in cartBus.carts.listcart)
        {
            product pro = c.Product;
            total += pro.Price * c.Quantity;
        }
        return total;
    }
    public int totalOrderproduct(int id)
    {
        DataTable dt = new DataTable();
        dt = ordal.getOrderByOrderID(id);
        return dt.Rows.Count;

    }
    
}